<?php

namespace app\api\controller;

use app\model\goods\Goods as GoodsModel;
use app\model\goods\GoodsLabel as GoodsLabelModel;
use app\model\system\Poster;
//use app\model\goods\Config as GoodsConfigModel;
use app\model\web\Config as ConfigModel;
use think\facade\Db;

class Goods extends BaseApi
{

    /**
     * 修改商品点击量
     * @return string
     */
    public function modifyclicks()
    {
        $sku_id = isset($this->params['sku_id']) ? $this->params['sku_id'] : 0;

        if (empty($sku_id)) {
            return $this->response($this->error('', 'REQUEST_SKU_ID'));
        }

        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);

        $goods_model = new GoodsModel();
        $res         = $goods_model->modifyClick($sku_id, $this->site_id);
        return $this->response($res);
    }

    /**
     * 获取商品海报
     */
    public function poster()
    {
        if (!empty($qrcode_param)) return $this->response($this->error('', '缺少必须参数qrcode_param'));

        $promotion_type                = 'null';
        $qrcode_param                  = json_decode($this->params['qrcode_param'], true);
        $qrcode_param['source_member'] = $qrcode_param['source_member'] ?? 0;
        $poster                        = new Poster();
        $res                           = $poster->goods($this->params['app_type'], $this->params['page'], $qrcode_param, $promotion_type, $this->site_id);
        return $this->response($res);
    }

    /**
     * 获取热门搜索关键词
     */
    public function hotSearchWords()
    {
        $config_model = new ConfigModel();
        $info = $config_model->getHotSearchWords($this->site_id, $this->app_module);
        return $this->response($this->success($info[ 'data' ][ 'value' ]));
    }

    /**
     * 获取默认搜索关键词
     */
    public function defaultSearchWords()
    {
        $config_model = new ConfigModel();
        $info = $config_model->getDefaultSearchWords($this->site_id, $this->app_module);
        return $this->response($this->success($info[ 'data' ][ 'value' ]));
    }

    /**
     * 售后保障
     * @return false|string
     */
    public function aftersale()
    {
//        $goods_config_model = new GoodsConfigModel();
//        $res                = $goods_config_model->getAfterSaleConfig($this->site_id);
//        return $this->response($res);
        $data = [
            'code'      =>  0,
            'message'   =>  '操作成功',
            'data'      =>  [],
        ];

        return $this->response($data);
    }

    public function label_lists()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);

        $goods_label_model = new GoodsLabelModel();
        $label_list        = $goods_label_model->getLabelList([['site_id', '=', $this->site_id]], 'id as label_id,label_name', 'create_time desc');

        return $this->response($label_list);
    }

    public function attr_lists()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);

        $list = Db::name('goods_attr_class')->field('class_id,class_name')->select();

        $data = [
            'code'      =>  0,
            'message'   =>  '操作成功',
            'data'      =>  $list,
        ];

        return $this->response($data);
    }
}